/**
 * Created by xuweijie on 17-5-15.
 */
$(function () {
   $('#loginsub').click(function () {
       var username=$('#username').val();
       var passwd=$('#password').val();
       var type = validlogin(username,passwd);
       if(type!==false){
           if (type!=3) {
               var params = $.param({
                   username: username,
                   password: passwd,
                   type: type
               }, true);
               $.ajax({
                   url: '/frontlogin.action',
                   type: 'post',
                   data: params,
                   dateType: 'json',
                   success: function (data) {
                       if (data == "0") {
                           window.location.href = "/front/index/showindex";
                       } else if (data == "1") {
                           alert("请输入正确的账号");
                           window.location.href = "/front/frontlogin";
                       } else {
                           alert("请输入正确的密码");
                           window.location.href = "/front/frontlogin";
                       }
                   }
               });
           }else {
               alert("账号格式不正确");
           }
       }else {
           alert("请输入正确的账号或密码");
       }
   })
});

$(function () {
   $('#loginreset') .click(function () {
       $('#username').val("");
       $('#password').val("");
   })
});

function validlogin(username,passwd) {
    var flag = false;
    if(username==="" || passwd===""){
        flag = false;
    }else if(username.indexOf('@')!==-1){
        flag = 1;
    }else if( /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(username)){
        flag = 2;
    }else{
        flag = 3;
    }
    return flag;
}